package pl.un4giv3n.mightvsmagic.utils.display;

import java.math.BigDecimal;

import org.displaytag.decorator.TableDecorator;

import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.utils.StatsUtil;
import pl.un4giv3n.mightvsmagic.utils.UserStats;

public class DisplayRankDecorator extends TableDecorator {

	public long getIndex() {
		int numItemsPerPage = 100;
		int page = 0;
		try {
			page = Integer.parseInt(getPageContext().getRequest().getParameter(
					"page"));
		} catch (NumberFormatException e) {
			// do nothing
		}
		int index = getListIndex();
		int currPage = page > 0 ? page - 1 : page;
		return (currPage * numItemsPerPage) + index + 1;
	}

	public long getFightsWon() {
		UserStats userStats = getUserStats();
		return userStats.getFightsWon();
	}

	public long getFightsLost() {
		UserStats userStats = getUserStats();
		return userStats.getFightsLost();
	}

	public double getGoldLost() {
		UserStats userStats = getUserStats();
		return userStats.getGoldLost();
	}

	public double getGoldWon() {
		UserStats userStats = getUserStats();
		return userStats.getGoldWon();
	}

	public double getDmgDone() {
		UserStats userStats = getUserStats();
		double result = new BigDecimal(userStats.getDmgDone()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return result;
	}

	public double getDmgTaken() {
		UserStats userStats = getUserStats();
		double result = new BigDecimal(userStats.getDmgTaken()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return result;
	}

	private UserStats getUserStats() {
		User user = (User) getCurrentRowObject();
		UserStats userStats = StatsUtil.getCachedStats().get(user.getId());
		return userStats;
	}
}
